package com.zjc.yohoshop.mvp.view.fragment;

import android.database.Cursor;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.zjc.lib_core.base.BaseFragment;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.CreateOrderEntity;
import com.zjc.yohoshop.dao.USerService;
import com.zjc.yohoshop.di.compont.DaggerCreateOrderFragmentCompont;
import com.zjc.yohoshop.di.modules.CreateOrderFragmentModules;
import com.zjc.yohoshop.mvp.contract.CreateOrderFragmentContract;
import com.zjc.yohoshop.mvp.persenter.CreateOrderFragmentPresenter;
import com.zjc.yohoshop.mvp.view.adapter.CreateOrderAdapter;
import com.zjc.yohoshop.mvp.view.adapter.CreateOrderViewPagerAdapter;
import com.zjc.yohoshop.mvp.view.custom.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

public class CreateOrderFragment extends BaseFragment<CreateOrderFragmentPresenter> implements CreateOrderFragmentContract.CreateOrderFragmentIView, View.OnClickListener {

    //TODO 主控件
    private RadioGroup rg_create_order;
    private NoScrollViewPager nsvp_create_order;
    private List<View> list = new ArrayList<>();
    private CreateOrderViewPagerAdapter adapter;
    //TODO 普通订单控件
    private View view_ptdd;
    private RadioGroup rg_item_create_order_pt;
    private ViewPager vp_item_create_order_pt;
    private CreateOrderViewPagerAdapter adapter_pt;
    //全部
    private LinearLayout ll_item_create_order_pt_vp_item_all;
    private PullToRefreshLayout ptrl_item_create_order_pt_all;
    private RecyclerView rv_item_create_order_pt_vp_item_all;
    private CreateOrderAdapter adapter_pt_all;
    //TODO 全球购订单控件
    private View view_qqgdd;
    private RadioGroup rg_item_create_order_qqg;
    private ViewPager vp_item_create_order_qqg;
    private CreateOrderViewPagerAdapter adapter_qqg;
    private PullToRefreshLayout ptrl_item_create_order_qqg_all;
    //TODO UFO订单控件
    private View view_ufodd;
    private RadioGroup rg_item_create_order_ufo;
    private ViewPager vp_item_create_order_ufo;
    private PullToRefreshLayout ptrl_item_create_order_ufo_all;
    private View view;

    @Override
    public View createView(LayoutInflater inflater) {
        view = View.inflate(getContext(), R.layout.fragment_create_order, null);
        return view;
    }

    @Override
    public void init(View view) {
        rg_create_order = view.findViewById(R.id.rg_create_order);
        nsvp_create_order = view.findViewById(R.id.nsvp_create_order);
        //TODO 普通订单控件初始化
        view_ptdd = View.inflate(getContext(), R.layout.item_create_order_pt, null);
        rg_item_create_order_pt = view_ptdd.findViewById(R.id.rg_item_create_order_pt);
        vp_item_create_order_pt = view_ptdd.findViewById(R.id.vp_item_create_order_pt);

        List<View> list_pt = new ArrayList<>();
        View view_pt_all = View.inflate(getContext(), R.layout.item_create_order_pt_vp_item_all, null);
        list_pt.add(view_pt_all);
        list_pt.add(View.inflate(getContext(), R.layout.item_create_order_pt_vp_item, null));
        list_pt.add(View.inflate(getContext(), R.layout.item_create_order_pt_vp_item, null));
        list_pt.add(View.inflate(getContext(), R.layout.item_create_order_pt_vp_item, null));
        adapter_pt = new CreateOrderViewPagerAdapter(list_pt);
        vp_item_create_order_pt.setAdapter(adapter_pt);

        //普通订单全部
        ll_item_create_order_pt_vp_item_all = view_pt_all.findViewById(R.id.ll_item_create_order_pt_vp_item_all);
        ptrl_item_create_order_pt_all = view_pt_all.findViewById(R.id.ptrl_item_create_order_pt_vp_item_all);
        ptrl_item_create_order_pt_all.setCanLoadMore(false);
        rv_item_create_order_pt_vp_item_all = view_pt_all.findViewById(R.id.rv_item_create_order_pt_vp_item_all);
        adapter_pt_all = new CreateOrderAdapter(R.layout.item_create_order);
        rv_item_create_order_pt_vp_item_all.setAdapter(adapter_pt_all);
        rv_item_create_order_pt_vp_item_all.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter_pt_all.addFooterView(View.inflate(getContext(), R.layout.item_create_order_vp_footer, null));

        //TODO 全球购订单控件初始化
        view_qqgdd = View.inflate(getContext(), R.layout.item_create_order_qqg, null);
        rg_item_create_order_qqg = view_qqgdd.findViewById(R.id.rg_item_create_order_qqg);
        vp_item_create_order_qqg = view_qqgdd.findViewById(R.id.vp_item_create_order_qqg);

        List<View> list_qqg = new ArrayList<>();
        list_qqg.add(View.inflate(getContext(), R.layout.item_create_order_pt_vp_item, null));
        list_qqg.add(View.inflate(getContext(), R.layout.item_create_order_pt_vp_item, null));
        list_qqg.add(View.inflate(getContext(), R.layout.item_create_order_pt_vp_item, null));
        list_qqg.add(View.inflate(getContext(), R.layout.item_create_order_pt_vp_item, null));
        adapter_qqg = new CreateOrderViewPagerAdapter(list_qqg);
        vp_item_create_order_qqg.setAdapter(adapter_qqg);
        //TODO UFO订单控件初始化
        view_ufodd = View.inflate(getContext(), R.layout.item_create_order_ufo, null);
        rg_item_create_order_ufo = view_ufodd.findViewById(R.id.rg_item_create_order_ufo);
        vp_item_create_order_ufo = view_ufodd.findViewById(R.id.vp_item_create_order_ufo);

        list.add(view_ptdd);
        list.add(view_qqgdd);
        list.add(view_ufodd);
        adapter = new CreateOrderViewPagerAdapter(list);
        nsvp_create_order.setAdapter(adapter);
    }

    @Override
    public void initData() {
        Cursor cursor = USerService.getInstance().query("user", null, null);
        if (cursor.moveToNext()) {
            String user_id = cursor.getString(cursor.getColumnIndex("user_id"));
            p.request(user_id);
        }
    }

    @Override
    public void setupDagger() {
        DaggerCreateOrderFragmentCompont.builder().createOrderFragmentModules(new CreateOrderFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        view.findViewById(R.id.iv_create_order_back).setOnClickListener(this);
        //主RadioGroup按钮监听
        rg_create_order.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_create_order_ptdd:
                        nsvp_create_order.setCurrentItem(0);
                        break;
                    case R.id.rb_create_order_qqgdd:
                        nsvp_create_order.setCurrentItem(1);
                        break;
                    case R.id.rb_create_order_ufodd:
                        nsvp_create_order.setCurrentItem(2);
                        break;
                }
            }
        });
        rg_create_order.check(R.id.rb_create_order_ptdd);
        //TODO 普通订单RadioGroup按钮监听
        rg_item_create_order_pt.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_item_create_order_pt_all://全部
                        vp_item_create_order_pt.setCurrentItem(0);
                        break;
                    case R.id.rb_item_create_order_pt_obligation://待付款
                        vp_item_create_order_pt.setCurrentItem(1);
                        break;
                    case R.id.rb_item_create_order_pt_await_send://待发货
                        vp_item_create_order_pt.setCurrentItem(2);
                        break;
                    case R.id.rb_item_create_order_pt_await_reception://待收货
                        vp_item_create_order_pt.setCurrentItem(3);
                        break;
                }
            }
        });
        //默认选中
        rg_item_create_order_pt.check(R.id.rb_item_create_order_pt_all);
        //普通订单刷新监听
        ptrl_item_create_order_pt_all.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                ptrl_item_create_order_pt_all.finishRefresh();
            }

            @Override
            public void loadMore() {

            }
        });
        //TODO 全球购订单RadioGroup按钮监听
        rg_item_create_order_qqg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_item_create_order_qqg_all://全部
                        vp_item_create_order_qqg.setCurrentItem(0);
                        break;
                    case R.id.rb_item_create_order_qqg_obligation://待付款
                        vp_item_create_order_qqg.setCurrentItem(1);
                        break;
                    case R.id.rb_item_create_order_qqg_await_send://待发货
                        vp_item_create_order_qqg.setCurrentItem(2);
                        break;
                    case R.id.rb_item_create_order_qqg_await_reception://待收货
                        vp_item_create_order_qqg.setCurrentItem(3);
                        break;
                }
            }
        });
        //默认选中
        rg_item_create_order_qqg.check(R.id.rb_item_create_order_qqg_all);
        //TODO UFO订单RadioGroup按钮监听
        rg_item_create_order_ufo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_item_create_order_ufo_all://全部
                        break;
                    case R.id.rb_item_create_order_ufo_obligation://待付款
                        break;
                    case R.id.rb_item_create_order_ufo_await_send://待发货
                        break;
                    case R.id.rb_item_create_order_ufo_await_reception://待收货
                        break;
                }
            }
        });
        //默认选中
        rg_item_create_order_ufo.check(R.id.rb_item_create_order_ufo_all);

    }

    @Override
    public void success(CreateOrderEntity entity) {
        if (entity == null) {
            return;
        }

        if (entity.values.size() > 0){
            ll_item_create_order_pt_vp_item_all.setVisibility(View.GONE);
            ptrl_item_create_order_pt_all.setVisibility(View.VISIBLE);
            adapter_pt_all.setNewData(entity.values);
        }else{
            ll_item_create_order_pt_vp_item_all.setVisibility(View.VISIBLE);
            ptrl_item_create_order_pt_all.setVisibility(View.GONE);
        }

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_create_order_back:
                getActivity().finish();
                break;
        }
    }
}
